<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 权限列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <el-input v-model="GetShow.name" placeholder="请输入内容" class="handle-input mr10"></el-input>
            <el-button type="primary" icon="el-icon-search" @click="GetPermissions">搜索</el-button>
            <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="PermissionsDeletes">批量删除</el-button>
        </div>
        <div>
            <el-table
                :data="tableData"
                ref="multipleTable"
                style="width: 100%; margin-bottom: 20px"
                row-key="id"
                border
                default-expand-all
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column prop="id" label="编号" sortable width="100"> </el-table-column>
                <el-table-column prop="permissionsName" label="权限名称" sortable width="180"> </el-table-column>
                <el-table-column prop="permissionsDescription" label="权限描述" sortable width="180"> </el-table-column>
                <el-table-column prop="orderSort" label="排序" sortable width="80"> </el-table-column>
                <el-table-column prop="apiId" label="Api接口" sortable width="180"> </el-table-column>
                <el-table-column prop="routeId" label="路由接口" sortable width="180"> </el-table-column>
                <el-table-column prop="isButton" label="是否为按钮" sortable width="100">
                    <template slot-scope="aaa">
                        <el-tag type="success" v-if="aaa.row.isButton == true">是</el-tag>
                        <el-tag type="danger" v-if="aaa.row.isButton == false">否</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="IsHide" label="是否是隐藏菜单" sortable width="100">
                    <template slot-scope="aaa">
                        <el-tag type="success" v-if="aaa.row.isHide == true">是</el-tag>
                        <el-tag type="danger" v-if="aaa.row.isHide == false">否</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="func" label="按钮事件" sortable width="180"> </el-table-column>
                <el-table-column prop="enabled" label="激活状态" sortable width="100">
                    <template slot-scope="aaa">
                        <el-tag type="success" v-if="aaa.row.enabled == true">是</el-tag>
                        <el-tag type="danger" v-if="aaa.row.enabled == false">否</el-tag>
                    </template>
                </el-table-column>
                <el-table-column>
                    <template slot-scope="aaa">
                        <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="AddHideTrue(aaa.row)"
                            >新增子权限</el-button
                        >
                        <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="GetPermissionsById(aaa.row)"
                            >编辑</el-button
                        >
                        <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="PermissionsDelete(aaa.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div>
            <!-- 权限添加弹出框 -->
            <el-dialog title="添加" :visible.sync="AddHide" width="30%">
                <el-form ref="form" :model="form" label-width="120px">
                    <el-form-item label="权限名称">
                        <el-input v-model="form.PermissionsName"></el-input>
                    </el-form-item>
                    <el-form-item label="所属权限">
                        <el-input v-model="form.Permissions_Pid"></el-input>
                    </el-form-item>
                    <el-form-item label="权限描述">
                        <el-input type="textarea" v-model="form.PermissionsDescription"></el-input>
                    </el-form-item>
                    <el-form-item label="排序">
                        <el-input v-model="form.OrderSort"></el-input>
                    </el-form-item>
                    <el-form-item label="Api接口">
                        <el-input v-model="form.ApiId"></el-input>
                    </el-form-item>
                    <el-form-item label="路由接口">
                        <el-input v-model="form.RouteId"></el-input>
                    </el-form-item>
                    <el-form-item label="是否是按钮">
                        <el-radio-group v-model="form.IsButton">
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="是否是隐藏菜单">
                        <el-radio-group v-model="form.IsHide">
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="按钮事件" v-if="form.IsButton == true">
                        <el-input v-model="form.Func"></el-input>
                    </el-form-item>
                    <el-form-item label="激活状态">
                        <el-radio-group v-model="form.Enabled">
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="AddHide = false">取 消</el-button>
                    <el-button type="primary" @click="PermissionsAdd">确 定</el-button>
                </span>
            </el-dialog>
        </div>
        <div>
            <!-- 权限编辑弹出框 -->
            <el-dialog title="编辑" :visible.sync="UpdateHide" width="30%">
                <el-form ref="form" :model="formline" label-width="120px">
                    <el-form-item label="权限名称">
                        <el-input v-model="formline.PermissionsName"></el-input>
                    </el-form-item>
                    <el-form-item label="所属权限">
                        <el-input v-model="formline.Permissions_Pid"></el-input>
                    </el-form-item>
                    <el-form-item label="权限描述">
                        <el-input type="textarea" v-model="formline.PermissionsDescription"></el-input>
                    </el-form-item>
                    <el-form-item label="排序">
                        <el-input v-model="formline.OrderSort"></el-input>
                    </el-form-item>
                    <el-form-item label="Api接口">
                        <el-input v-model="formline.ApiId"></el-input>
                    </el-form-item>
                    <el-form-item label="路由接口">
                        <el-input v-model="formline.RouteId"></el-input>
                    </el-form-item>
                    <el-form-item label="是否是按钮">
                        <el-radio-group v-model="formline.IsButton">
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="是否是隐藏菜单">
                        <el-radio-group v-model="formline.IsHide">
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="按钮事件" v-if="formline.IsButton == true">
                        <el-input v-model="formline.Func"></el-input>
                    </el-form-item>
                    <el-form-item label="激活状态">
                        <el-radio-group v-model="formline.Enabled">
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="UpdateHide = false">取 消</el-button>
                    <el-button type="primary" @click="PermissionsUpdate">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>
<script>
import { getpermissionsList, getpermissionsById, Postpermissions, UpdatepermissionsList } from '@/api/permissions.js/';

export default {
    data() {
        return {
            //权限列表查询对象
            GetShow: {
                name: ''
            },
            //权限列表集合
            tableData: [],
            //是否弹出添加弹出框
            AddHide: false,
            //是否弹出编辑弹出框
            UpdateHide: false,
            //复选框
            multipleSelection: [],
            //权限添加的对象
            form: {
                PermissionsName: '',
                Permissions_Pid: undefined,
                PermissionsDescription: '',
                OrderSort: undefined,
                ApiId: '',
                RouteId: '',
                IsButton: true,
                IsHide: false,
                Func: '',
                Enabled: true
            },
            //权限修改的对象
            formline: {
                PermissionsName: '',
                Permissions_Pid: undefined,
                PermissionsDescription: '',
                OrderSort: undefined,
                ApiId: '',
                RouteId: '',
                IsButton: true,
                IsHide: false,
                Func: '',
                Enabled: true
            }
        };
    },
    created() {
        //权限列表显示
        this.GetPermissions();
    },
    methods: {
        //复选框值
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection);
        },
        //权限列表显示
        GetPermissions() {
            getpermissionsList().then((res) => {
                this.tableData = res.data;
            });
        },
        //弹出权限添加from表单
        AddHideTrue(row) {
            this.form = {};
            this.form.Permissions_Pid = row.Id;
            this.AddHide = true;
        },
        //权限添加
        PermissionsAdd() {
            Postpermissions(this.form).then((res) => {
                if (res == 1) {
                    //录入成功！
                    this.$message.success('录入成功！');
                    this.AddHide = false;
                    this.GetPermissions();
                } else {
                    this.$message({
                        showClose: true,
                        message: '录入失败！',
                        type: 'error'
                    });
                }
            });
        },
        //查询权限单条数据
        GetPermissionsById(row) {
            this.UpdateHide = true;
            let obj = {
                id: row.Id
            };
            getpermissionsById(obj).then((res) => {
                this.formline = res;
            });
        },
        //权限编辑
        PermissionsUpdate() {
            UpdatepermissionsList(this.formline).then((res) => {
                if (res == 1) {
                    //编辑成功！
                    this.$message.success('编辑成功！');
                    this.UpdateHide = false;
                    this.GetPermissions();
                } else {
                    this.$message({
                        showClose: true,
                        message: '编辑失败！',
                        type: 'error'
                    });
                }
            });
        },
        //权限单条删除
        PermissionsDelete(row) {
            console.log(row.Id);
        },
        //权限批量删除
        PermissionsDeletes() {
            let arr = [];
            this.multipleSelection.forEach((e) => {
                arr.push(e.Id);
            });
            console.log(arr);
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
